package com.tsinghuabigdata.edu.ddmath.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;


import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.bean.Question;

import java.util.HashMap;
import java.util.List;



/**
 * Created by fish on 2016/10/20.
 */

public class DayQuestionsAdapter extends RecyclerView.Adapter {

    private Context                   mContext;
    private List<Question>            mList;
    private HashMap<Integer, Boolean> mHashMap;

    public DayQuestionsAdapter(Context context, List<Question> data) {
        mContext = context;
        mList = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(mContext).inflate(R.layout.item_day_questions, parent, false);
        return new RankViewHolder(root);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        RankViewHolder holder1 = (RankViewHolder) holder;
        Question vo = mList.get(position);
        if (position % 2 == 0) {
            holder1.iv.setImageResource(R.drawable.hanlanda);
        } else {
            holder1.iv.setImageResource(R.drawable.img_ad1);
        }
        Boolean select = mHashMap.get(position);
        holder1.cb.setChecked(select);
        //        holder1.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        //            @Override
        //            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //                Log.i("sky", "onCheckedChanged position=" + position + " isChecked=" + isChecked);
        //                mHashMap.put(position, isChecked);
        //            }
        //        });
        holder1.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkBox = (CheckBox) v;
                mHashMap.put(position, checkBox.isChecked());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setData(List<Question> list) {
        mList.clear();
        mList.addAll(list);
        Log.i("sky", "list.size()=" + list.size());
        Log.i("sky", "mList.size()=" + mList.size());
    }


    public void setMap(HashMap<Integer, Boolean> hashMap) {
        mHashMap = hashMap;
        for (int i = 0; i < mList.size(); i++) {
            if (mHashMap.get(i) == null) {
                mHashMap.put(i, false);
            }
        }
    }

    class RankViewHolder extends RecyclerView.ViewHolder {
        ImageView iv;
        CheckBox  cb;

        public RankViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv_question);
            cb = (CheckBox) itemView.findViewById(R.id.cb_select_questions);
        }
    }

}
